package me.zhengjie;

import java.util.Random;

public class IDCardGenerator {

    private static final String[] ADDRESS_CODES = {"110105", "120101", "310101", "500103", "610103", "440106", "320102", "210102", "130102", "330102"};
    private static final Random RANDOM = new Random();

    public static String generateIDCard() {
        // 随机选择一个地址码
        String addressCode = ADDRESS_CODES[RANDOM.nextInt(ADDRESS_CODES.length)];

        // 随机生成出生日期
        int year = 1950 + RANDOM.nextInt(70); // 1950-2020
        int month = 1 + RANDOM.nextInt(12); // 1-12
        int day = 1 + RANDOM.nextInt(28); // 1-28，避免闰月和大月问题
        String birthDate = String.format("%04d%02d%02d", year, month, day);

        // 随机生成顺序码
        int sequence = RANDOM.nextInt(1000); // 000-999
        String sequenceCode = String.format("%03d", sequence);

        // 拼接前17位
        String baseIDCard = addressCode + birthDate + sequenceCode;

        // 计算校验码
        int[] weights = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char[] checkCodes = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (baseIDCard.charAt(i) - '0') * weights[i];
        }

        char checkCode = checkCodes[sum % 11];

        // 返回完整的身份证号码
        return baseIDCard + checkCode;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            System.out.println(generateIDCard());
        }
    }
}
